GBML manual / 3D

3D view

How a room becomes a 3D corridor you walk through: the view from inside, drawn from the room's tiles by code, with walls, a flat floor and ceiling, walking and turning.

A room seen from inside

A top-down room is a grid of tiles. The 3D view draws the same grid as a place you stand in: every solid tile is a wall that rises in front of you, every other tile is open floor, and you look along it from a camera that you can walk and turn. It is how corridor shooters and dungeon crawlers are drawn, and it needs nothing in the Room Editor: you paint the room the way you always do, and a little code switches the view on.

It is GBML only and Advance only. On the other consoles the functions are grayed out, with the reason.

How it works, in plain words

For each of 120 columns across the screen, the console sends a ray out from the camera, straight along the line of sight of that column. The ray steps from tile edge to tile edge until it hits a solid tile. How far it went tells how tall that wall looks: near walls are tall, far walls are short. Where along the wall the ray hit tells which slice of the wall picture to copy into that column. Above the wall the column is the ceiling color, below it the floor color. Do that for all 120 columns, each 2 pixels wide, and you have a picture of the whole view. That is the entire trick, and it is why this kind of 3D is fast enough for a small console: no solid shapes, only one short ray per column.

The picture is drawn into two screens that take turns, so you never see it half finished, once a frame.

Setting it up

  1. Paint the room. Make a room of up to 64 by 64 tiles. Paint solid tiles where the walls go; the room's edges are walls too. The tile you paint decides the wall's picture: see Making 3D walls.
  2. Make the wall pictures. One picture in your Tilesets, 32 by 32 pixels for each wall.
  3. Start the view in the Create event of one object:
view3d_start(ts_walls);
view3d_camera(100, 100, 0);
view3d_colors(#303848, #6A5038);
  1. Walk and turn in that object's Step event:
if (action_held(up)) { view3d_move(1.5, 0); }
if (action_held(down)) { view3d_move(-1.5, 0); }
if (action_held(left)) { view3d_turn(3); }
if (action_held(right)) { view3d_turn(-3); }

That is a complete game of walking around. view3d_move stops at walls and slides along them; see Collision in 3D.

Enemies, items, doors and shooting

The walls are only the stage. Enemies and items in 3D puts things in it (a picture standing on the floor that shrinks with distance and hides behind walls), asks whether one place can see another, finds what a shot hits, and slides doors open. For a game with a HUD, view3d_height stops the view above it.

Numbers to know

WhatValue
Picture size120 columns, each 2 pixels wide, by 160 rows
Room sizeup to 64 by 64 tiles
Wall pictures32 by 32 pixels, up to 64
Colorsone set of 256 for the whole view: the wall pictures share what is left after the floor and ceiling
Positionsroom pixels from the top-left corner; a tile is 8
Anglesdegrees, counter-clockwise: 0 looks right, 90 looks up the room
View width66 degrees, change it with view3d_width (30 to 120)
View heightthe whole screen (160 rows); view3d_height makes it shorter and leaves the rows below black for a HUD

What to keep in mind

See also: Making 3D wallsCollision in 3DEnemies and items in 3DSpeed: why 30 framesview3d_heightview3d_shadingview3d_startview3d_cameraview3d_moveview3d_turnview3d_colorsview3d_widthPerspective floor